from django.db.models.base import pre_save, post_save
from django.dispatch import receiver

from .models import Project, MODEL_AUDIT_PROGRESS_ORDER
from audit.models import AuditManagement


@receiver(post_save)
def post_save_project_related(sender, instance, created, **kwargs):
    if created:
        if sender == Project:
            AuditManagement.objects.get_or_create_for_object(instance, tag=f"project-{instance.id}")
        elif sender.__name__ in MODEL_AUDIT_PROGRESS_ORDER:
            AuditManagement.objects.get_or_create_for_object(instance, tag=f"project-{instance.project.id}")

